<?php
/**
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: Helper.php 3986 2010-07-25 16:32:46Z quanvo $
 * @since        2.0.8
 */

 class Tomato_Helper_Helper{

    /**
    * Smart subtring
    * @param: string $source_string - String be to cut
    * @param: in $count Is String lenght be to cut
    * @return: string
    **/
    public static function substring($source_string, $count){

        /**
        * Check string is unicode
        **/
        if (strlen($source_string) != strlen(utf8_decode($source_string))){
            $count = $count+15;
        }

        if (strlen($source_string) <= $count)
            return $source_string;
        $source = $source_string;
        $secondStr = substr($source, 0 , $count);
        if (strpos($secondStr, " ")) {
            $newIdx = strrpos($secondStr, " ");
        } else {
            $newIdx = strlen($secondStr);
        }
        $result = substr($source, 0, $newIdx);

        if (strlen($result) < strlen($source_string))
            $result .= "...";
        return $result;
    }

    /**
     * Check file exist in directory
     * Return new file
     * @param: $path Path file to directory
     * example: $path = 'C:\wamp\www\hoalan\upload\page\admin\2010\11\4ce0f0a438085_large.jpg'
     * @param: $file Path file formt server
     * example: $file = 'http://localhost/hoalan/upload/page/admin/2010/11/4ce0f0a438085_large.jpg'
     * @param: $name New file name
     * $name = '_square' is FROm 4ce0f0a438085_large.jpg TO 4ce0f0a438085_square.jpg
     * @param $noimage 
     * true is show else not show
     * @return string
    **/
    public static function checkFile($path, $file, $name = '_square'){
        $new_file = null;
        if(is_file($path)){
            $pathinfo = pathinfo($path);
            $arrEx = explode('_',$file);
            $forReplace = $arrEx[count($arrEx)-1];
            $new_file = str_replace('_'.$forReplace, $name.'.'.$pathinfo['extension'], $file);
        }
        return $new_file;
    }
 }
?>
